#!/usr/bin/env node

const https = require('https'),
      http = require('http'),
      url = require('url'),
      addr ='https://api.seniverse.com/v3/weather/now.json?key=SMC5XgncsqvC4DKu7&location=shijiazhuang&language=zh-Hans&unit=c'; 
let result = '';
https.get(global.encodeURI(addr),(res)=>{

  res.on('data',(data)=>{
    result +=data;
  });
  res.on('end',()=>{
    //let weather = JSON.parse(result);
    console.log(result);
  });
});
console.log('OK');
http.createServer((req,res)=>{
  let adr = url.parse(req.url);

  if(adr.pathname === '/wether'){
    res.writeHead(200,{
      'Content-Type':'application/json',
      'Content-length':Buffer.byteLength(result)
    });
    res.end(result);
  }
  res.end('OK');
}).listen(8080);
